/**
 * 
 */
package ads.engenharia.dal;

import java.sql.ResultSet;
import java.sql.SQLException;

import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;
import ads.engenharia.entity.Schedule.Day;
import ads.engenharia.entity.Schedule.Hour;
import ads.engenharia.entity.Schedule.Time;

/**
 * @author Lanedraex
 *
 */
public class ProfessorScheduleDAO {

	private String sql;
	private String tableName = "\"Professor_Schedule\"";

	private EntityManager em = new EntityManager() {

		@Override
		public Object treatResultSet(ResultSet resultSet) throws SQLException {

			Schedule schedule = new Schedule();

			schedule.Days.add(Day.values()[resultSet.getInt("day") - 1]);
			schedule.Times.add(Time.values()[resultSet.getInt("time") - 1]);
			schedule.Hours.add(Hour.values()[resultSet.getInt("hour") - 1]);

			return schedule;
		}
	};

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void save(Professor professor) throws SQLException {

		sql = " insert into " + tableName
				+ " ( professor_id, day, time, hour ) values ( ?, ?, ?, ? ) ";

		for (int count = 0; count < professor.schedule().Days.size(); ++count) {

			em.executeQuery(sql, professor.id(),
					professor.schedule().Days.get(count).value(),
					professor.schedule().Times.get(count).value(),
					professor.schedule().Hours.get(count).value());
		}
	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void update(Professor professor) throws SQLException {

		sql = " update " + tableName
				+ " set day = ?, time = ?, hour = ? where professor_id = ?";

		for (Schedule.Day day : professor.schedule().Days) {

			for (Schedule.Time time : professor.schedule().Times) {

				for (Schedule.Hour hour : professor.schedule().Hours) {

					em.executeQuery(sql, day.value(), time.value(),
							hour.value(), professor.id());
				}
			}
		}
	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void delete(Professor professor) throws SQLException {

		sql = " delete from " + tableName + " where professor_id = ? ";

		for (int count = 0; count < professor.schedule().Days.size(); ++count) {

			em.executeQuery(sql, professor.id());
		}
	}

	/**
	 * 
	 * @param professor
	 * @return completedSchedule
	 * @throws SQLException
	 */
	public Schedule searchProfessorSchedule(Professor professor)
			throws SQLException {

		sql = " select distinct day, time, hour from " + tableName
				+ " where professor_id = ? order by day, time, hour ";

		return (Schedule) em.getSingleResult(sql, professor.id());
	}

	/**
	 * 
	 * @param id
	 * @return completedSchedule
	 * @throws SQLException
	 */
	public Schedule searchProfessorSchedule(int id) throws SQLException {

		sql = " select distinct day, time, hour from " + tableName
				+ " where professor_id = ? order by day, time, hour ";

		return (Schedule) em.getSingleResult(sql, id);
	}
}
